import { areaList } from '@vant/area-data';
import store from '../../utils/store'
import dayjs from "dayjs";
import router from "../../utils/router";
const app = getApp()
let Api = app.Api

Page({
    data: {
        areaList,
        name: '',
        job: '',
        area: '',
        desc: '',
        phoneNumber: '',
        show: false,
        nameErrMsg: '',
        jobErrMsg: '',
        areaErrMsg: '',
        descErrMsg: ''
    },
    onLoad: function (options) {
        const phoneNumber = store.getItem('phoneNumber')
        this.setData({
            phoneNumber
        })
    },
    // 点击地区显示弹窗
    handleClickArea(e) {
        this.setData({
            show: true
        })
    },
    // 点击空白处关闭弹窗
    onClose() {
        this.setData({
            show:false
        })
    },
    // 点击确认按钮
    handleConfirm(e) {
        const arr = e.detail.values.map(item => item.name)
        const area = arr.join('')
        this.setData({
            area,
            show: false
        })
    },
    // 点击取消按钮
    handleCancel(e) {
        this.setData({
            show: false
        })
    },
    handleDescriptionInput(e) {
        console.log(e)
        this.setData({
            desc: e.detail,
            descErrMsg: ''
        })
    },
    handleNameInput(e) {
        this.setData({
            name: e.detail,
            nameErrMsg: ''
        })
    },
    handleJobInput(e) {
        this.setData({
            job: e.detail,
            jobErrMsg: ''
        })
    },
    // 失去焦点对姓名和职务进行一个简单的不为空校验
    handleBlur(e) {
        const type = e.currentTarget.dataset.name
        const value = e.detail.value
        if (type === 'name') {
            if (!value) {
                this.setData({
                    nameErrMsg: '姓名不可为空'
                })
            }
        } else if (type === 'job') {
            if (!value) {
                this.setData({
                    jobErrMsg: '职务不可为空'
                })
            }else {
                this.setData({
                    job: value,
                })
            }
        }else if (type === 'desc') {
            if (!value) {
                this.setData({
                    descErrMsg: '水泥厂名称不可为空'
                })
            } else {
                this.setData({
                    desc: value,
                })
            }
        }
    },
    // 简单判断数据状态
    judgeData(name) {
        let text = '内容不能为空'
        switch (name) {
            case 'area':
                text = '地区不能为空'
                break
            case 'name':
                text = '姓名不能为空'
                break
            case 'job':
                text = '职务不能为空'
                break
            case 'desc':
                text = '水泥厂名称不能为空'
                break
        }
        if (!this.data[name]) {
            this.setData({
                [`${name}ErrMsg`]:text
            })
        } else if (this.data[name]){
            this.setData({
                [`${name}ErrMsg`]:''
            })
        }
    },
    // 提交函数
    handleSubmit() {
        this.judgeData('area')
        this.judgeData('name')
        this.judgeData('job')
        this.judgeData('desc')
        if (this.data.area && this.data.name && this.data.job &&this.data.phoneNumber && this.data.desc) {
            app.post(Api.addUserInfo, {
                userName: this.data.name,
                job: this.data.job,
                phoneNumber: this.data.phoneNumber,
                area: this.data.area,
                description: this.data.desc,
                createdAt: dayjs().format('YYYY-MM-DD')
            }).then((response) => {
                if (response.phoneNumber == this.data.phoneNumber) {
                    wx.request({
                        url: 'https://mingduzscloud.com/api/notify/admin',
                        data: {
                            cement_name: this.data.desc
                        },
                        method: 'POST',
                        header: {
                            'content-type': 'application/json'
                        },
                        success (res) {
                            if (res.data.code === 0) {
                                store.setItem("isExist", true)
                                router.push('company')
                            }
                        }
                    })
                }
            })
        } else {
            console.log('2')
        }
    }
});